<template>
  <div class="supplier-management">
    <!-- 操作栏 -->
    <div class="operation-bar">
      <el-input
        v-model="searchText"
        placeholder="搜索店铺ID/名称/类型"
        style="width: 360px"
        clearable
      >
        <template #prefix>
          <el-icon><Search /></el-icon>
        </template>
      </el-input>
      <el-button type="primary" @click="handleAdd">
        <el-icon><Plus /></el-icon>新增店铺
      </el-button>
    </div>

    <!-- 数据表格 -->
    <el-table 
      :data="filteredShops" 
      border 
      style="width: 100%"
      header-row-class-name="table-header"
      stripe
      highlight-current-row
    >
      <el-table-column prop="shopId" label="店铺ID" width="160" sortable align="center" />
      <el-table-column prop="shopName" label="店铺名称" width="180" />
      <el-table-column prop="owner" label="店主" width="120">
        <template #default="{ row }">
          <el-tag effect="plain" type="info">
            {{ row.owner }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="category" label="主营类型" width="140">
        <template #default="{ row }">
          <el-tag effect="plain" :type="categoryTagType[row.category]">
            {{ row.category }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="certification" label="店铺等级" width="140">
        <template #default="{ row }">
          <el-tag :type="certTagType[row.certification]" effect="light">
            {{ certOptions.find(s => s.value === row.certification)?.label }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="license" label="注册编号" width="200" />
      <el-table-column prop="openingDate" label="开业日期" width="140" sortable />
      <el-table-column label="操作" width="180" fixed="right" align="center">
        <template #default="{ row }">
          <el-button size="small" type="primary" plain @click="handleEdit(row)">编辑</el-button>
          <el-button
            size="small"
            type="danger"
            plain
            @click="handleDelete(row.shopId)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="isEditMode ? '编辑店铺信息' : '新增店铺'"
      width="680"
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="100px"
        label-position="left"
      >
        <el-form-item label="店铺ID" prop="shopId">
          <el-input 
            v-model="formData.shopId" 
            :disabled="isEditMode"
            placeholder="GS开头+7位数字"
          />
        </el-form-item>
        <el-form-item label="店铺名称" prop="shopName">
          <el-input v-model="formData.shopName" placeholder="请输入店铺全称"/>
        </el-form-item>
        <el-form-item label="店主信息" prop="owner">
          <el-input v-model="formData.owner" placeholder="店主姓名及联系方式"/>
        </el-form-item>
        <el-form-item label="主营类型" prop="category">
          <el-select v-model="formData.category" placeholder="请选择类型">
            <el-option
              v-for="item in categories"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="店铺等级" prop="certification">
          <el-select v-model="formData.certification" placeholder="请选择等级">
            <el-option
              v-for="item in certOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="注册编号" prop="license">
          <el-input v-model="formData.license" />
        </el-form-item>
        <el-form-item label="开业日期" prop="openingDate">
          <el-date-picker
            v-model="formData.openingDate"
            type="date"
            placeholder="选择日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 品类配置
const categories = [
  '手工艺品',
  '创意礼品',
  '定制饰品',
  '情侣礼品',
  '节日礼盒',
  '文创产品'
]

// 认证选项及样式
const certOptions = [
  { value: '五星', label: '五星店铺' },
  { value: '金牌', label: '金牌店铺' },
  { value: '优选', label: '优选店铺' },
  { value: '普通', label: '普通店铺' }
]

const certTagType = {
  '五星': 'success',
  '金牌': 'warning',
  '优选': 'danger',
  '普通': ''
}

// 品类标签样式映射
const categoryTagType = {
  '手工艺品': 'info',
  '创意礼品': 'danger',
  '定制饰品': 'success',
  '情侣礼品': 'warning',
  '节日礼盒': '',
  '文创产品': 'info'
}

// 初始店铺数据（10条）
const shops = ref([
  {
    shopId: 'GS2024001',
    shopName: '时光手作坊',
    owner: '林婉儿 13800001111',
    category: '手工艺品',
    certification: '五星',
    license: '91370200MA3F6B3K1C',
    openingDate: '2020-03-15'
  },
  {
    shopId: 'GS2024002',
    shopName: '星语心愿礼品屋',
    owner: '苏沐橙 13922223333',
    category: '创意礼品',
    certification: '金牌',
    license: '91370200MA3F4W8J8Y',
    openingDate: '2021-06-20'
  },
  {
    shopId: 'GS2024003',
    shopName: '唯一饰品定制',
    owner: '楚子航 18612345678',
    category: '定制饰品',
    certification: '优选',
    license: '91370200MA3F3R7J7X',
    openingDate: '2019-09-10'
  },
  {
    shopId: 'GS2024004',
    shopName: '甜蜜情侣馆',
    owner: '路明非 15987654321',
    category: '情侣礼品',
    certification: '五星',
    license: '91370200MA3F2T6K6W',
    openingDate: '2022-08-01'
  },
  {
    shopId: 'GS2024005',
    shopName: '节日魔法盒',
    owner: '陈雯雯 13700008888',
    category: '节日礼盒',
    certification: '金牌',
    license: '91370200MA3F1Q5J5V',
    openingDate: '2018-04-12'
  },
  {
    shopId: 'GS2024006',
    shopName: '故宫文创体验店',
    owner: '萧炎 18812341234',
    category: '文创产品',
    certification: '普通',
    license: '91370200MA3F0P4J4U',
    openingDate: '2023-11-05'
  },
  {
    shopId: 'GS2024007',
    shopName: 'DIY创意工坊',
    owner: '唐舞桐 13566778899',
    category: '手工艺品',
    certification: '优选',
    license: '91370200MA3FZ94J3T',
    openingDate: '2022-02-28'
  },
  {
    shopId: 'GS2024008',
    shopName: '银饰物语',
    owner: '霍雨浩 18609870987',
    category: '定制饰品',
    certification: '五星',
    license: '91370200MA3FY83J2S',
    openingDate: '2020-07-19'
  },
  {
    shopId: 'GS2024009',
    shopName: '情人节限定馆',
    owner: '比比东 13900998877',
    category: '情侣礼品',
    certification: '金牌',
    license: '91370200MA3FX72J1R',
    openingDate: '2021-03-10'
  },
  {
    shopId: 'GS2024010',
    shopName: '敦煌文创旗舰店',
    owner: '千仞雪 15843214321',
    category: '文创产品',
    certification: '普通',
    license: '91370200MA3FW61J0Q',
    openingDate: '2019-09-22'
  }
])

// 搜索功能
const searchText = ref('')
const filteredShops = computed(() => {
  return shops.value.filter(shop => {
    return Object.values(shop).some(value => 
      String(value).toLowerCase().includes(searchText.value.toLowerCase())
  )})
})

// 对话框相关状态
const dialogVisible = ref(false)
const isEditMode = ref(false)
const formRef = ref(null)
const formData = reactive({
  shopId: '',
  shopName: '',
  owner: '',
  category: '',
  certification: '普通',
  license: '',
  openingDate: new Date().toISOString().split('T')[0]
})

// 表单验证规则
const formRules = {
  shopId: [
    { required: true, message: '请输入店铺ID', trigger: 'blur' },
    { pattern: /^GS\d{7}$/, message: '格式要求：GS+7位数字', trigger: 'blur' }
  ],
  shopName: [{ required: true, message: '请输入店铺名称', trigger: 'blur' }],
  category: [{ required: true, message: '请选择主营类型', trigger: 'change' }],
  license: [
    { required: true, message: '请输入注册编号', trigger: 'blur' },
    { pattern: /^[A-Z0-9]{18}$/, message: '请输入有效注册编号', trigger: 'blur' }
  ],
  openingDate: [{ required: true, message: '请选择开业日期', trigger: 'change' }]
}

// 操作函数
const handleAdd = () => {
  isEditMode.value = false
  resetForm()
  dialogVisible.value = true
}

const handleEdit = (row) => {
  isEditMode.value = true
  Object.assign(formData, { ...row })
  dialogVisible.value = true
}

const handleDelete = (shopId) => {
  ElMessageBox.confirm('确定要删除该店铺吗？', '警告', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    shops.value = shops.value.filter(u => u.shopId !== shopId)
    ElMessage.success('删除成功')
  })
}

const submitForm = async () => {
  await formRef.value.validate()
  if (isEditMode.value) {
    const index = shops.value.findIndex(u => u.shopId === formData.shopId)
    shops.value.splice(index, 1, { ...formData })
  } else {
    if (shops.value.some(u => u.shopId === formData.shopId)) {
      return ElMessage.warning('店铺ID已存在')
    }
    shops.value.push({ ...formData })
  }
  dialogVisible.value = false
  ElMessage.success(isEditMode.value ? '修改成功' : '新增成功')
}

const resetForm = () => {
  formData.shopId = ''
  formData.shopName = ''
  formData.owner = ''
  formData.category = ''
  formData.certification = '普通'
  formData.license = ''
  formData.openingDate = new Date().toISOString().split('T')[0]
}
</script>

<style scoped lang="scss">
.supplier-management {
  padding: 24px;
  background: #f8fafc;
  min-height: 100vh;

  .operation-bar {
    padding: 16px 24px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(44, 62, 80, 0.08);
    margin-bottom: 24px;
    display: flex;
    align-items: center;
    gap: 16px;
  }

  :deep(.table-header) th {
    background-color: #f8fafc !important;
    color: #64748b;
    font-weight: 600;
    height: 56px;
  }

  .el-table {
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(44, 62, 80, 0.08);
    
    .el-tag {
      font-size: 13px;
      padding: 0 8px;
      height: 26px;
      line-height: 24px;
    }
  }
}

.el-dialog {
  border-radius: 12px;
  
  .el-dialog__header {
    border-bottom: 1px solid #e2e8f0;
    margin: 0 24px;
    padding: 24px 0 16px;
  }
  
  .el-dialog__body {
    padding: 24px 32px;
  }
  
  .el-dialog__footer {
    border-top: 1px solid #e2e8f0;
    padding: 16px 24px;
  }
}

.el-form-item {
  margin-bottom: 20px;
}
</style>